// Copyright 2024 The Google Research Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto2";

package eeg_modelling;

// This proto is intended to be a lossless representation of an EDF file.
//
// For details of EDF and EDF+:
// https://www.edfplus.info/specs/edf.html
// https://www.edfplus.info/specs/edfplus.html

message Edf {
  optional EdfHeader header = 1;
  repeated DataRecord data_records = 2;
}

message EdfHeader {
  // 8 bytes in an EDF file.
  optional string version = 1;

  // 80 bytes in an EDF file.
  message LocalPatientIdentification {
    optional string full_text = 1;
    optional string patient_id = 2;
    optional string sex = 3;
    optional string birthdate = 4;
    optional string name = 5;
  }
  optional LocalPatientIdentification local_patient_identification = 2;

  // 80 bytes in an EDF file.
  message LocalRecordingInformation {
    optional string full_text = 1;
    optional string start_date = 2;
    optional string investigation_code = 3;
    optional string investigator_code = 4;
    optional string equipment_code = 5;
  }
  optional LocalRecordingInformation local_recording_information = 3;

  // 8 bytes in an EDF file.
  optional string recording_start_date = 4;
  // 8 bytes in an EDF file.
  optional string recording_start_time = 5;
  // 8 bytes in an EDF file.
  optional uint32 num_header_bytes = 6;

  // 44 bytes of reserved.
  enum TypeFromReserved {
    UNSPECIFIED = 0;  // Usually indicates EDF.
    EDF_PLUS_C = 1;
    EDF_PLUS_D = 2;
  }
  // First 5 bytes of reserved field in EDF file.
  optional TypeFromReserved type_from_reserved = 7;

  // 8 bytes ascii in an EDF file.  Value of -1 indicates file was not completed
  // properly and indicates an invalid file.
  optional int32 num_data_records = 8;
  // 8 bytes in an EDF file.
  optional string num_seconds_per_data_record = 9;
  // 4 bytes in an EDF file.
  optional uint32 num_signals = 10;

  message SignalHeader {
    // 16 bytes in EDF file.
    optional string label = 1;
    // 80 bytes in EDF file.
    optional string transducer_type = 2;
    // 8 bytes in EDF file.
    optional string physical_dimension = 3;

    // 8 bytes in EDF file.
    optional string physical_min = 4;
    // 8 bytes in EDF file.
    optional string physical_max = 5;

    // digital_min > digital_max is valid in this case.
    // 8 bytes in EDF file.
    optional string digital_min = 6;
    // 8 bytes in EDF file.
    optional string digital_max = 7;

    // 80 bytes in EDF file.
    optional string prefiltering = 8;
    // 8 bytes in EDF file.
    optional uint32 num_samples_per_data_record = 9;

    // 32 bytes in EDF file reserved.
  }
  repeated SignalHeader signal_headers = 11;
}

// Represents a set of signals over the same timespan.
message DataRecord {
  repeated Signal signals = 1;
}

message Signal {
  oneof value {
    IntegerSignal integers = 1;
    AnnotationSignal annotations = 2;
    // In the future, we could add LongIntegerSignal to support BDF files.
  }
}

message IntegerSignal {
  // Should really be an int16, but not an option in protos.
  repeated int32 samples = 1;
}

message AnnotationSignal {
  repeated TimeStampedAnnotationList tals = 1;
}

message TimeStampedAnnotationList {
  optional string start_offset_seconds = 1;
  optional string duration_seconds = 2;
  repeated string annotations = 3;
}
